<?php
namespace App\Http\Controllers\backend;
use DB ;
Use App\models\User;
use Request;
use App\Http\Controllers\backend\Base;

class UserController extends Base
{
    public function actionIndex(){
        if(!$this->isJson){
            return $this->view('user.index');
        }
        return response()->json($this->page( User::query() ));
    }


    public function actionSave(){
        $id = Request::get('id',0);
        if(!$this->isPost){

            $data = '""' ;
            if($id){
                $data = User::find($id)->toJson();
            }

            return $this->view('user.save',['data' => $data]);
        }

        $data = Request::all() ;

        if($id){
            $model = User::find($id)->fill($data) ;
        }else{
            $model = new User($data);
        }

        if($model->validate($data) && $model->save()){
            return true ;
        }
        return $model ;
    }


    public function actionDelete(){
        $id = Request::get('id',0);
        $isDelete = User::find($id)->delete();
        return response()->json(['code' => 0]);
    }
}